import os from 'os-utils'
import disk from 'check-disk-space'

class System {
    private static cpuUsage: string;
    private static cpuFree: string;
    // 获取磁盘的基本信息
    public async getDiskInfo() {
        let currentPath = process.cwd() // 获取当前软件的安装路径
        let res = await disk(currentPath)
        let diskUsage = ((res.free/res.size)*100).toFixed(2)
        let size = (res.size/1024/1024/1024).toFixed(2)
        let free = (res.free/1024/1024/1024).toFixed(2)

        return {
            currentPath,
            diskPath:res.diskPath,
            size,
            free,
            diskUsage
        }
    }

    // 获取系统内存信息
    public async getMemInfo() {
        let totalMem = os.totalmem().toFixed(2) // 获取总内存
        let freeMem = totalMem - os.freemem().toFixed(2) // 获取可用内存
        let freememPercentage = ((1-os.freememPercentage())*100).toFixed(2) // 获取内存利用率

        return {
            totalMem,
            freememPercentage,
            freeMem
        }
    }

    // 获取系统cpu
    public async getCpuInfo() {
        let platform:string = os.platform() // 系统类型window 或者liunx
        let cpuCount = os.cpuCount() //获取内核数
        os.cpuUsage((v) => {
            System.cpuUsage = (v*100).toFixed(2)
        })
        os.cpuFree((v) => {
            System.cpuFree = (v*100).toFixed(2)
        })

        return {
            platform,
            cpuCount,
            cpuUsage: System.cpuUsage,
            cpuFree: System.cpuFree
        }
    }
}

export default System
